package com.pwc.conversionTool.task;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;

import org.dom4j.DocumentException;

import com.pwc.conversionTool.IO.Reader;

public class TaskManage {

	private static TaskManage taskManage=null;
	
	private static List<Task> tasks=null;
	
	public static TaskManage getInstance()
	{
		if(taskManage==null)
		{
			taskManage=new TaskManage();
			tasks=new ArrayList<Task>();
		}
		
		return taskManage;
	}

	
	
	private TaskManage()
	{}
	
	
	public static List<Task> getTasks() {
		return tasks;
	}
	
	/**
	 * 
	 * @param folderPath
	 * @param operation
	 * @param relationPath
	 * @param TaskName
	 */
	public void startTask(String folderPath,String operation,String relationPath,String TaskName ){
		List<File>files= Reader.getAllFileFromStringPath(folderPath);
		startTask(files,  operation, relationPath, TaskName);
		}
	
	
	
	/**
	 * to start a task from this function and add it to 
	 * @param files
	 * @param operation
	 * @param relationPath
	 * @param TaskName
	 */
	public void startTask(List<File> files,String operation,String relationPath,String TaskName){
		Task task=new Task();
		//some setting 
		
		tasks.add(task);
		
		task.run();
	}
	
	//
	public void startTask(ServletContext context,List<String> csvFiles,String relationFile,String taskName) throws IOException, DocumentException{
		Task task=new Task(context,csvFiles,relationFile,taskName);
		task.Initial();
		//some setting 
		
		Thread th=new Thread(task,taskName);
		
		tasks.add(task);
		
		th.start();
//		task2.run();
	}
	
	
	
	
//	public void testTask() throws InterruptedException{
//		Task task=new Task();
//		Thread task1=new Thread(task, "task1");
////		Thread task2=new Thread(task, "task2");
////		Thread task3=new Thread(task, "task3");
////		Thread task4=new Thread(task, "task4");
//		task1.start();
//		
//		task1.sleep(1000);
//		task.stop=true;
////		task2.start();
////		task3.start();
////		task4.start();
//		
////		task.run();
//	}
	
	public void testThread() throws InterruptedException{
		taskThread th1=new taskThread();
		th1.start();
		System.out.println(th1.stop);
//		th1.stop=true;
	}
	
	
	public void stopTask(){
		
	}


	
	
	
	
	
	
	
}
